package com.qdlc.p2p.manage.action.additional;

import com.opensymphony.xwork2.ModelDriven;
import com.qdlc.p2p.biz.service.CashAwardRuleService;
import com.qdlc.p2p.biz.web.action.BaseAction;
import com.qdlc.p2p.common.exception.BusinessException;
import com.qdlc.p2p.common.util.DateUtil;
import com.qdlc.p2p.dal.common.model.jpa.PageDataList;
import com.qdlc.p2p.dal.dto.CashAwardRule;
import com.qdlc.p2p.dal.dto.Operator;
import com.qdlc.p2p.dal.model.CashAwardRuleModel;
import org.apache.struts2.convention.annotation.Action;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * 管理现金奖励规则
 *
 * @author Kaiju Zhao
 * @date 2016-3-23
 */
public class ManageCashAwardRuleAction extends BaseAction<CashAwardRuleModel> implements ModelDriven<CashAwardRuleModel> {

    @Resource
    private CashAwardRuleService cashAwardRuleService;

    private CashAwardRuleModel model = new CashAwardRuleModel();

    public CashAwardRuleModel getModel() {
        return model;
    }

    private Map<String, Object> data;

    /**
     * 进入现金奖励规则页面
     *
     * @author Kaiju Zhao
     * @date 2016-3-23
     */
    @Action("/modules/additional/cashAwardRule/cashAwardRuleManager")
    public String cashAwardRuleManager() {
        return "cashAwardRuleManager";
    }

    /**
     * 获取所有现金奖励规则列表
     *
     * @author Kaiju Zhao
     * @date 2016-3-23
     */
    @Action("/modules/additional/cashAwardRule/cashAwardRuleList")
    public void cashAwardRuleList() throws IOException {
        data = new HashMap<String, Object>();
        PageDataList<CashAwardRuleModel> pageDataList = cashAwardRuleService.list(model);
        data.put("total", pageDataList.getPage().getTotal());
        data.put("rows", pageDataList.getList());
        printJson(getStringOfJpaObj(data));
    }

    /**
     * 进入添加现金奖励规则页面
     *
     * @return
     * @author Kaiju Zhao
     * @date 2016-3-23
     */
    @Action("/modules/additional/cashAwardRule/cashAwardRuleAddPage")
    public String cashAwardRuleAddPage() {
        return "cashAwardRuleAddPage";
    }

    /**
     * 添加现金奖励规则
     *
     * @return
     * @throws Exception
     * @author Kaiju Zhao
     * @date 2016-3-23
     */
    @SuppressWarnings("static-access")
    @Action("/modules/additional/cashAwardRule/cashAwardRuleAdd")
    public void cashAwardRuleAdd() throws Exception {
        checkCashAwardRuleModel(model);

        Operator operator = getOperator();
        if (null != operator) {
            model.setCreator(operator.getId());
            model.setCreateTime(DateUtil.getNow());
        }
        int cashAwardRule = cashAwardRuleService.saveCashAwardRule(model);
        if (cashAwardRule<=0) {
            printResult("添加现金奖励规则失败！", false);
            return;
        }
        printResult("添加现金奖励规则成功！", true);
    }

    /**
     * 修改现金奖励规则页面
     *
     * @return
     * @throws Exception
     * @author Kaiju Zhao
     * @date 2016-3-23
     */
    @Action("/modules/additional/cashAwardRule/cashAwardRuleEditPage")
    public String cashAwardRuleEditPage() throws Exception {
        CashAwardRuleModel cashAwardRule = cashAwardRuleService.findCashAwardRuleById(model.getId());
        if (null == cashAwardRule) {
            printResult("该现金奖励规则不存在!", false);
            return "cashAwardRuleManager";
        }
        request.setAttribute("cashAwardRule", cashAwardRule);
        return "cashAwardRuleEditPage";
    }

    /**
     * 修改现金奖励规则
     *
     * @return
     * @throws Exception
     * @author Kaiju Zhao
     * @date 2016-3-23
     */
    @Action("/modules/additional/cashAwardRule/cashAwardRuleEdit")
    public void cashAwardRuleEdit() throws Exception {
        checkCashAwardRuleModel(model);

        Operator operator = getOperator();
        if (null == operator) {
            printResult("请先登录！", false);
            return;
        }
        model.setModifier(operator.getId());
        model.setModifyTime(DateUtil.getNow());
        int cashAwardRule = cashAwardRuleService.updateCashAwardRule(model);
        if (cashAwardRule <=0) {
            printResult("修改现金奖励规则失败！", false);
            return;
        }
        printResult("修改现金奖励规则成功！", true);
    }

    /**
     * 查看现金奖励规则详情页面
     *
     * @return
     * @throws Exception
     * @author Kaiju Zhao
     * @date 2016-3-23
     */
    @Action("/modules/additional/cashAwardRule/cashAwardRuleDetailPage")
    public String cashAwardRuleDetailPage() throws Exception {
        CashAwardRuleModel cashAwardRule = cashAwardRuleService.findCashAwardRuleById(model.getId());
        if (null == cashAwardRule) {
            printResult("该现金奖励规则不存在!", false);
            return "cashAwardRuleManager";
        }
        request.setAttribute("cashAwardRule", cashAwardRule);
        return "cashAwardRuleDetailPage";
    }

    /**
     * 开启现金奖励规则
     *
     * @return
     * @throws IOException
     * @author Kaiju Zhao
     * @date 2016-3-23
     */
    @Action("/modules/additional/cashAwardRule/cashAwardRuleEnable")
    public void cashAwardRuleEnable() throws IOException {
        Operator operator = getOperator();
        if (null == operator) {
            printResult("请先登录！", false);
            return;
        }
        model.setModifier(operator.getId());
        model.setModifyTime(DateUtil.getNow());
        int result = cashAwardRuleService.toggleStatus(model);
        if (result > 0) {
            printWebSuccess();
        } else {
            printWebResult("启用失败，请确认活动规则是否已开启！", false);
        }
    }

    /**
     * 关闭现金奖励规则
     *
     * @return
     * @throws IOException
     * @author Kaiju Zhao
     * @date 2016-3-23
     */
    @Action("/modules/additional/cashAwardRule/cashAwardRuleDisable")
    public void cashAwardRuleDisable() throws IOException {
        Operator operator = getOperator();
        if (null == operator) {
            printResult("请先登录！", false);
            return;
        }
        model.setModifier(operator.getId());
        model.setModifyTime(DateUtil.getNow());
        int result = cashAwardRuleService.toggleStatus(model);
        if (result > 0) {
            printWebSuccess();
        } else {
            printWebResult("关闭失败，请确认活动规则是否已关闭！", false);
        }
    }

    /**
     * 校验现金奖励规则
     *
     * @param model
     * @author Kaiju Zhao
     * @date 2016-3-23
     */
    private void checkCashAwardRuleModel(CashAwardRuleModel model) throws Exception {
        if (null == model) {
            throw new BusinessException("设置现金奖励规则模型为空!", BusinessException.TYPE_JSON);
        }

        int status = model.getStatus();
        if (CashAwardRuleModel.CASH_AWARD_RULE_STATUS_ENABLE != status
                && CashAwardRuleModel.CASH_AWARD_RULE_STATUS_DISABLE != status) {
            throw new BusinessException("设置现金奖励规则状态不合法!", BusinessException.TYPE_JSON);
        }

        double cashBackRate = model.getCashBackRate();
        if (cashBackRate < 0D|| cashBackRate > 1D) {
            throw new BusinessException("设置现金奖励规则返现率不合法!", BusinessException.TYPE_JSON);
        }
    }
}